package com.controller.admin;

import com.bean.vo.admin.Result;
import com.bean.vo.user.AllUserVO;
import com.google.gson.Gson;
import com.service.admin.AdminServiceImplement;
import com.service.admin.UserManagementServiceImplement;
import com.utils.GsonUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/admin/user/*")
public class UserServlet extends HttpServlet {
    private static final Gson gson= GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/user/", "");
        if ("allUser".equals(action)){
            showAllUser(request,response);
        }
        if ("deleteUser".equals(action)){
            deleteUser(request,response);
        }
        if ("searchUser".equals(action)){
            searchUserByNickname(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
/**
 * @Author xiao-fan
 * @Description  按昵称User
 * @Date 10:51 2021/6/17
 **/
    private void searchUserByNickname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String nickname = request.getParameter("word");
        UserManagementServiceImplement userManagementServiceImplement = new UserManagementServiceImplement();
        List<AllUserVO> allUserVOList = userManagementServiceImplement.searchByNickname(nickname);
        response.getWriter().println(gson.toJson(new Result(0,allUserVOList,null)));
    }

    /**
 * @Author xiao-fan
 * @Description  删除用户操作
 * @Date 10:28 2021/6/17
 **/
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id=Integer.parseInt(idString);
        UserManagementServiceImplement userManagementServiceImplement = new UserManagementServiceImplement();
        int deleteState = userManagementServiceImplement.deleteUser(id);
        if (deleteState==200){
            Result result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
        }
        else{
            response.getWriter().println(gson.toJson(new Result(10000,null,"你不能删除admin账号!")));
        }
    }
    /**
     * @Author xiao-fan
     * @Description  展示所有用户信息
     * @Date 10:03 2021/6/17
     **/
    private void showAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserManagementServiceImplement userManagementServiceImplement = new UserManagementServiceImplement();
        List<AllUserVO> allUserVOList = userManagementServiceImplement.showAllUser();
        Result result = new Result(0, allUserVOList, null);
        response.getWriter().println(gson.toJson(result));

    }
}
